/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
 
Ext.ns('net.autodata');
//Only allow one instance of the Session object
(function() {

    //Grab this data once and only once
    if (window.ASC_config) {
        var AC = window.ASC_config;
    }

    var _sessionData = AC.session || {};
    var _pageUrls = AC.comparePageUrls || {};

    var SessionImpl = Ext.extend(Object, {
        constructor: function() {
            var eb = net.autodata.EventBus.getInstance();
            var EVENTS = net.autodata.EventBus.EVENTS;

            var competitorStore = net.autodata.compare.data.CompetitorDataStore.getInstance();

            //private class member variables only accessible to methods/properties declared in the return object
            var _vehicles = new net.autodata.compare.data.CompareVehicles({
                baseVehicle: _sessionData.baseVehicle,
                competitors: _sessionData.competitors,

                listeners: {

                    'baseimagechanged': function(vehicle) {
                        eb.fireEvent(EVENTS.SESSION_VEHICLE_IMAGE_CHANGED, vehicle);
                    },

                    'competitorimagechanged': function(index, vehicle) {
                        eb.fireEvent(EVENTS.SESSION_VEHICLE_IMAGE_CHANGED, vehicle);
                    },

                    'competitorchanged': function(index, competitor, prevCompetitor) {
                        eb.fireEvent(EVENTS.SESSION_COMPETITOR_CHANGED, index, competitor, prevCompetitor);
                    },

                    'competitorremoved': function(index, competitor) {
                        eb.fireEvent(EVENTS.SESSION_COMPETITOR_REMOVED, index, competitor);
                    }
                }
            });

            competitorStore.on({
                'loadcomplete': function(index, competitor) {
                    _vehicles.setCompetitor(index, competitor);
                },

                'loadimagescomplete': function(images) {
                    var competitors = _vehicles.getCompetitors();
                    Ext.each(images, function(image, index) {
                        if (image) {
                            competitors.setSelectedImageName(index, image.imageName);
                        }
                    });
                },

                'loadexception': function(errorMessage) {
                    eb.fireEvent(EVENTS.LOAD_EXCEPTION, errorMessage);
                }
            });

            eb.on(EVENTS.CHANGE_BASE_IMAGE, function(swatch) {
                _vehicles.setBaseVehicleImage(swatch);
            });

            //return the public interface for this class
            return {
                vehicles: _vehicles,

                getPageUrls: function() {
                    return _pageUrls;
                }
            }
        }
    });

    Ext.apply(SessionImpl, {
        getInstance: function() {
            var instance;
            return function() {
                if (!instance) {
                    instance = new SessionImpl();
                }

                return instance;
            };
        } ()
    });

    net.autodata.Session = SessionImpl;

})();